import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { Router } from '@angular/router'
import { CommonUtil } from 'src/app/common/utils/common.util'
import { LoginService } from 'src/app/common/services/login.service'
import { CoolLocalStorage } from 'src/app/common/coolStorage/cool-local-storage'

@Component({
  selector: 'app-register-psw',
  templateUrl: './register-psw.component.html',
  styleUrls: ['./register-psw.component.scss']
})
export class RegisterPswComponent implements OnInit {
  // 账号
  account = ''
  // 密码
  password = ''
  // 登录中
  loging = false

  constructor(
    private location: Location,
    private router: Router,
    private utils: CommonUtil,
    public loginService: LoginService,
    private storage: CoolLocalStorage
  ) {}

  ngOnInit() {}

  // 返回上一页
  onClickLeft() {
    this.location.back()
  }
  // 登录请求
  loginRequest() {
    let reqdata = {
      website: 'CowX.House1.Consumer.WXMP',
      token: this.account,
      account: this.account,
      password: this.password
    }
    this.loging = true
    this.loginService.loginWithPsw(
      reqdata,
      res => {
        let msg = '登录失败，请重试'
        this.loging = false
        if (res.Success && res.Result) {
          this.storage.setItem('token', res.Result)
          this.router.navigate(['/workspace/home/index'])
        } else {
          if (res.Message && res.Message.indexOf('|') > -1) {
            msg = res.Message.split('|')[0]
          }
          this.utils.info(msg)
        }
      },
      () => {
        this.loging = false
        this.utils.fail('网络异常，请重试')
      }
    )
  }
}
